package com.cpecc.dao.wuzi;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;

import com.cpecc.model.system.Dep;
import com.cpecc.model.system.User;
import com.cpecc.model.wuzi.ShenQing;
import com.cpecc.model.wuzi.WuZi;

@Repository
public class ShenQingDao {

	@Resource
	private SessionFactory sessionFactory;
	
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	
	public void save(ShenQing shenQing){
		Session session=sessionFactory.getCurrentSession();
		session.save(shenQing);
		session.flush();
	}
	
	@SuppressWarnings("unchecked")
	public List<ShenQing> getMyShenQing(User user){
		String hql="from ShenQing t where t.user.id=? and t.huanJie.id < 113 order by t.shenQingRQ desc";
		Query query=sessionFactory.getCurrentSession().createQuery(hql);
		query.setString(0, user.getId());
		return query.list();
	}
	
	@SuppressWarnings("unchecked")
	public List<ShenQing> getAllInDep(Dep dep){
		String hql="from ShenQing t where t.dep.id=? and t.huanJie.id=102";
		Query query=sessionFactory.getCurrentSession().createQuery(hql);
		query.setString(0, dep.getId());
		return query.list();
	}
	
	public ShenQing get(String id){
		return (ShenQing) sessionFactory.getCurrentSession().get(ShenQing.class, id);
	}
	
	public boolean cover(ShenQing shenQing){
		String hql="update ShenQing t set t.huanJie=?,t.wuZi=?,t.dangQianShu=?,t.zongShu=?,t.caiMaiR=?,t.dep=? where t.id=?";
		Query query=sessionFactory.getCurrentSession().createQuery(hql);
		query.setParameter(0, shenQing.getHuanJie());
		query.setParameter(1, shenQing.getWuZi());
		query.setInteger(2, shenQing.getDangQianShu());
		query.setInteger(3, shenQing.getZongShu());
		query.setParameter(4, shenQing.getCaiMaiR());
		query.setParameter(5, shenQing.getDep());
		query.setString(6, shenQing.getId());
		return query.executeUpdate()>0;
	}
	
	@SuppressWarnings("unchecked")
	public List<ShenQing> getAllYanShouRK(){
		String hql="from ShenQing t where t.huanJie.id=105 or t.huanJie.id=203 order by t.shenQingRQ desc";
		Query query=sessionFactory.getCurrentSession().createQuery(hql);		
		return query.list();
	}
	
	@SuppressWarnings("unchecked")
	public List<ShenQing> getAllCaiGouZT(int zhuangT){
		String hql="from ShenQing t where t.huanJie.id=? order by t.shenQingRQ desc";
		Query query=sessionFactory.getCurrentSession().createQuery(hql);
		query.setInteger(0, zhuangT);
		return query.list();
	}
	
	@SuppressWarnings("unchecked")
	public List<ShenQing> getAllCaiGouZT2CMR(int zhuangT, User user){
		String hql="from ShenQing t where t.huanJie.id=? and t.caiMaiR=? order by t.shenQingRQ desc";
		Query query=sessionFactory.getCurrentSession().createQuery(hql);
		query.setInteger(0, zhuangT);
		query.setParameter(1, user);
		return query.list();
	}
	
	public ShenQing getShenQingForWuZi(WuZi wuZi){
		String hql="from ShenQing t where t.wuZi=?";
		Query query=sessionFactory.getCurrentSession().createQuery(hql);
		query.setParameter(0, wuZi);
		return (ShenQing) query.list().get(0);
	}
	
	@SuppressWarnings("unchecked")
	public List<ShenQing> getAllLingYong(User user){
		String hql="from ShenQing t where t.huanJie.id=107 and t.wuZi.user.id=? order by t.shenQingRQ desc";
		Query query=sessionFactory.getCurrentSession().createQuery(hql);
		query.setString(0, user.getId());
		return query.list();
	}
	
	public boolean hasLiuCheng(User user){
		String hql="from ShenQing t where t.user=? and (t.huanJie.id<107 or t.huanJie.id=111)";
		Query query=sessionFactory.getCurrentSession().createQuery(hql);
		query.setParameter(0, user);
		return query.list().size()>0;
	}
	
	@SuppressWarnings("unchecked")
	public List<ShenQing> getShenQing(User user){
		String hql="from ShenQing t where t.user=? and t.huanJie.id=102";
		Query query=sessionFactory.getCurrentSession().createQuery(hql);
		query.setParameter(0, user);
		return query.list();
	}
	
	public boolean cheHui(String id){
		String hql="update ShenQing t set t.huanJie.id=? where t.id=?";
		Query query=sessionFactory.getCurrentSession().createQuery(hql);
		query.setInteger(0, 111);
		query.setString(1, id);
		return query.executeUpdate()>0;
	}
	
	@SuppressWarnings("unchecked")
	public List<ShenQing> activeShenQing(){
		String hql="from ShenQing t where t.huanJie.id<110";
		Query query=sessionFactory.getCurrentSession().createQuery(hql);
		return query.list();
	}
	
	public User getDaiJieShouUser(String shenQingId){
		String hql="select t.wuZi.user from ShenQing t where t.id=?";
		Session session=sessionFactory.openSession();
		Query query=session.createQuery(hql);
		query.setString(0, shenQingId);
		User user=new User();
		if(query.list().size()>0)
			user=(User) query.list().get(0);
		session.close();
		return user;
	}
	
	public WuZi getDaiJieShouWuZi(String shenQingId){
		String hql="select t.wuZi from ShenQing t where t.id=?";
		Session session=sessionFactory.openSession();
		Query query=session.createQuery(hql);
		query.setString(0, shenQingId);
		WuZi wuZi=new WuZi();
		if(query.list().size()>0){
			WuZi temp=(WuZi) query.list().get(0);
			wuZi.setZiChanBH(temp.getZiChanBH());
			wuZi.setWuZiMC(temp.getWuZiMC());
			wuZi.setPinPai(temp.getPinPai());
			wuZi.setGuiGeXH(temp.getGuiGeXH());
			wuZi.setGouMaiRQ(temp.getGouMaiRQ());
		}
		session.close();
		return wuZi;
	}
	
	public String getDaiJieShouDep(String shenQingId){
		String hql="select t.wuZi.dep.depName from ShenQing t where t.id=?";
		Session session=sessionFactory.openSession();
		Query query=session.createQuery(hql);
		query.setString(0, shenQingId);
		String result="";
		if(query.list().size()>0)
			result=(String) query.list().get(0);
		session.close();
		return result;
	}
	
	public boolean cover1(String shenQingId){
		String hql="update ShenQing t set t.huanJie.id=108 where t.id=?";
		Session session=sessionFactory.openSession();
		Query query=session.createQuery(hql);
		query.setString(0, shenQingId);
		boolean result=query.executeUpdate()>0;
		session.close();
		return result;
	}
	
	public ShenQing get1(String shenQingId){
		ShenQing shenQing=new ShenQing();
		Session session=sessionFactory.openSession();
		ShenQing temp=(ShenQing) session.get(ShenQing.class, shenQingId);
		if(temp!=null){
			shenQing.setId(temp.getId());
		}
		session.close();
		return shenQing;
	}

	public boolean delete(String id) {
		String hql="delete ShenQing t where t.id=?";
		Query query=sessionFactory.getCurrentSession().createQuery(hql);
		query.setString(0, id);
		return query.executeUpdate()>0;
	}
}
